import request from '@/utils/request'

// 赛事数据类型定义
export interface Match {
  id?: number
  tournament_name: string
  tournament_type: string
  tournament_score: number
  tournament_intro: string
  venue: string
  match_time: string
  status: 'pending' | 'ongoing' | 'completed' | 'cancelled'
  remarks: string
  created_at?: string
  updated_at?: string
}

// 查询参数类型
export interface MatchQueryParams {
  page?: number
  limit?: number
  tournament_name?: string
  tournament_intro?: string
  tournament_score?: number
  remarks?: string
  tournament_type?: string
  status?: string
  start_date?: string
  end_date?: string
}

// 分页响应类型
export interface PaginationResponse {
  current: number
  pageSize: number
  total: number
  pages: number
}

// API响应类型
export interface ApiResponse<T = any> {
  success: boolean
  data: T
  message: string
  error?: string
}

// 列表响应类型
export interface MatchListResponse {
  list: Match[]
  pagination: PaginationResponse
}

/**
 * 获取赛事列表
 */
export const getMatchList = (params: MatchQueryParams = {}): Promise<ApiResponse<MatchListResponse>> => {
  return request({
    url: '/api/matches/list',
    method: 'get',
    params
  })
}

/**
 * 获取赛事详情
 */
export const getMatchDetail = (id: number): Promise<ApiResponse<Match>> => {
  return request({
    url: `/api/matches/${id}`,
    method: 'get'
  })
}

/**
 * 创建赛事
 */
export const createMatch = (data: Omit<Match, 'id' | 'created_at' | 'updated_at'>): Promise<ApiResponse<{ id: number }>> => {
  return request({
    url: '/api/matches',
    method: 'post',
    data
  })
}

/**
 * 更新赛事
 */
export const updateMatch = (id: number, data: Partial<Match>): Promise<ApiResponse> => {
  return request({
    url: `/api/matches/${id}`,
    method: 'put',
    data
  })
}

/**
 * 删除赛事
 */
export const deleteMatch = (id: number): Promise<ApiResponse> => {
  return request({
    url: `/api/matches/${id}`,
    method: 'delete'
  })
}

/**
 * 批量删除赛事
 */
export const batchDeleteMatches = (ids: number[]): Promise<ApiResponse<{ deletedCount: number }>> => {
  return request({
    url: '/api/matches/batch',
    method: 'delete',
    data: { ids }
  })
}
